<?php
declare (strict_types=1);

namespace app\listener;


use Swoole\Server;
use Swoole\Server\Task;
use think\Container;
use think\swoole\Table;

class SwooleTask
{

    protected $server = null;
    protected $table = null;

    public function __construct(Server $server, Container $container)
    {
        $this->server = $server;
        $this->table = $container->get(Table::class);
    }

    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle(Task $task)
    {
//        var_dump('on task');
//        var_dump($task->data);
        if (isset($task->data['cmd']) && $task->data['cmd'] == 'ping')
            $this->ping();
        return;
    }

    protected function ping()
    {
        foreach ($this->table->u2fd as $row) {
            $this->server->push($row['fd'], 'ping');
        }
    }
}
